package com.haobangs.imms.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.config.MyLambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.haobangs.imms.mapper.DeviceMapper;
import com.haobangs.imms.domain.Device;
import com.haobangs.imms.service.IDeviceService;

/**
 * 设备管理Service业务层处理
 *
 * @author haobangs
 * @date 2023-06-11
 */
@Service
public class DeviceServiceImpl implements IDeviceService
{
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public Device selectDeviceById(Long id)
    {
        return deviceMapper.selectById(id);
    }

    /**
     * 分页查询设备管理
     *
     * @param device 设备管理
     * @return Page 对象
     */
    public Page getByPage(Device device){
        MyLambdaQueryWrapper<Device> wrapper = new MyLambdaQueryWrapper<>();
                    wrapper.eqIfPresent(Device::getFactoryId, device.getFactoryId());
                    wrapper.eqIfPresent(Device::getWorkshopId, device.getWorkshopId());
                    wrapper.eqIfPresent(Device::getEdgeId, device.getEdgeId());
                    wrapper.likeIfPresent(Device::getDeviceName, device.getDeviceName());
                    wrapper.eqIfPresent(Device::getDeviceSerialNumber, device.getDeviceSerialNumber());
                    wrapper.eqIfPresent(Device::getStatus, device.getStatus());
        Page<Device> devices = deviceMapper.selectPage(new Page<>(device.getPageNum(), device.getPageSize()), wrapper);
        return devices;
    }

    /**
     * 查询设备管理列表
     *
     * @param device 设备管理
     * @return 设备管理
     */
    @Override
    public List<Device> selectDeviceList(Device device)
    {
        MyLambdaQueryWrapper<Device> wrapper = new MyLambdaQueryWrapper<>();
                    wrapper.eqIfPresent(Device::getFactoryId, device.getFactoryId());
                    wrapper.eqIfPresent(Device::getWorkshopId, device.getWorkshopId());
                    wrapper.eqIfPresent(Device::getEdgeId, device.getEdgeId());
                    wrapper.likeIfPresent(Device::getDeviceName, device.getDeviceName());
                    wrapper.eqIfPresent(Device::getDeviceSerialNumber, device.getDeviceSerialNumber());
                    wrapper.eqIfPresent(Device::getStatus, device.getStatus());
        return deviceMapper.selectList(wrapper);
    }

    /**
     * 新增设备管理
     *
     * @param device 设备管理
     * @return 结果
     */
    @Override
    public int insertDevice(Device device)
    {
        device.setCreateTime(DateUtils.getNowDate());
        return deviceMapper.insert(device);
    }

    /**
     * 修改设备管理
     *
     * @param device 设备管理
     * @return 结果
     */
    @Override
    public int updateDevice(Device device)
    {
        device.setUpdateTime(DateUtils.getNowDate());
        return deviceMapper.updateById(device);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByIds(Long[] ids)
    {
        if(ids.length == 0){
            throw new RuntimeException("id不能为空");
        }
        return deviceMapper.deleteBatchIds(Arrays.asList(ids));

    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteDeviceById(Long id)
    {
        return deviceMapper.deleteById(id);
    }

    @Override
    public Device selectDeviceBySerialNumber(String deviceSerialNumber) {
        if(StringUtils.isNotNull(deviceSerialNumber)){
            return deviceMapper.selectOne(new QueryWrapper<Device>().eq("device_serial_number",deviceSerialNumber));
        }else {
            throw new RuntimeException("设备序列号为空");
        }
    }

    @Override
    public Map<String, String> selectDeviceStatus() {
        List<Device> devices = deviceMapper.selectList(new QueryWrapper<Device>().select("device_serial_number", "status"));
        Map<String, String> statusMap = devices.stream()
                .collect(Collectors.toMap(Device::getDeviceSerialNumber, Device::getStatus,
                        (oldValue, newValue) -> newValue));
        return statusMap;
    }
}
